{% extends 'base/base.html' %}

{% block title %}仪表盘 - 多用户报警系统{% endblock %}

{% block content %}
<div class="mb-4">
    <h1 class="mb-4">
        <i class="fa fa-dashboard" aria-hidden="true"></i> 仪表盘
    </h1>
    
    <div class="row">
        <!-- 欢迎卡片 -->
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h2 class="h4">欢迎回来，{{ user.username }}！</h2>
                </div>
                <div class="card-body">
                    <p class="mb-0">您上次登录时间：{{ user.last_login|date:"Y年m月d日 H:i:s" }}</p>
                    {% if user.phone %}
                        <p class="mb-0">您的手机号：{{ user.phone }}</p>
                    {% endif %}
                    {% if user.email %}
                        <p class="mb-0">您的邮箱：{{ user.email }}</p>
                    {% endif %}
                    <div class="mt-3">
                        <a href="{% url 'accounts:profile_edit' %}" class="btn btn-primary btn-sm">
                            <i class="fa fa-pencil" aria-hidden="true"></i> 编辑个人资料
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 用户统计卡片 -->
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h2 class="h4">我的项目统计</h2>
                </div>
                <div class="card-body">
                    <div class="row text-center">
                        <div class="col-6">
                            <div class="p-3 rounded-lg bg-primary bg-opacity-10">
                                <i class="fa fa-folder-open-o" aria-hidden="true" style="font-size: 2rem; color: #1677ff;"></i>
                                <h3 class="h2 mt-2">{{ user_projects.count }}</h3>
                                <p class="text-muted">参与项目</p>
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="p-3 rounded-lg bg-success bg-opacity-10">
                                <i class="fa fa-user-circle-o" aria-hidden="true" style="font-size: 2rem; color: #52c41a;"></i>
                                <h3 class="h2 mt-2">{{ user_created_projects.count }}</h3>
                                <p class="text-muted">创建项目</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 最近项目 -->
    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h2 class="h4 mb-0">
                <i class="fa fa-history" aria-hidden="true"></i> 最近项目
            </h2>
            <a href="{% url 'projects:project_list' %}" class="btn btn-primary btn-sm">
                <i class="fa fa-list" aria-hidden="true"></i> 查看全部
            </a>
        </div>
        <div class="card-body">
            {% if recent_projects %}
                <div class="list-group">
                    {% for project in recent_projects %}
                        <a href="{% url 'projects:project_detail' project.id %}" class="list-group-item list-group-item-action flex-column align-items-start">
                            <div class="d-flex w-100 justify-content-between">
                                <h5 class="mb-1">{{ project.name }}</h5>
                                <span class="text-muted small">
                                    <span class="role-badge role-{{ project.user_role }}">{{ project.get_user_role_display }}</span>
                                </span>
                            </div>
                            <p class="mb-1 text-muted small">{{ project.description|truncatechars:100 }}</p>
                            <div class="d-flex w-100 justify-content-between">
                                <span class="text-muted small">
                                    <i class="fa fa-clock-o" aria-hidden="true"></i> {{ project.updated_at|date:"Y年m月d日 H:i" }}
                                </span>
                                {% if project.tags.count > 0 %}
                                    <div class="d-flex gap-2">
                                        {% for tag in project.tags.all|slice:3 %}
                                            <span class="tag" style="background-color: {{ tag.color }};">{{ tag.name }}</span>
                                        {% endfor %}
                                        {% if project.tags.count > 3 %}
                                            <span class="tag bg-gray-500">+{{ project.tags.count|add:"-3" }}</span>
                                        {% endif %}
                                    </div>
                                {% endif %}
                            </div>
                        </a>
                    {% endfor %}
                </div>
            {% else %}
                <div class="text-center py-4">
                    <i class="fa fa-folder-open-o" aria-hidden="true" style="font-size: 3rem; color: #d9d9d9;"></i>
                    <p class="mt-3 text-muted">您还没有参与任何项目</p>
                    <a href="{% url 'projects:project_create' %}" class="btn btn-primary mt-2">
                        <i class="fa fa-plus" aria-hidden="true"></i> 创建第一个项目
                    </a>
                </div>
            {% endif %}
        </div>
    </div>
    
    <!-- Webhook清单 -->
    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h2 class="h4 mb-0">
                <i class="fa fa-plug" aria-hidden="true"></i> Webhook清单
            </h2>
            <a href="#" class="btn btn-primary btn-sm">
                <i class="fa fa-list" aria-hidden="true"></i> 查看全部
            </a>
        </div>
        <div class="card-body">
            {% if user_webhooks %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Webhook名称</th>
                                <th>所属项目</th>
                                <th>渠道</th>
                                <th>状态</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for webhook in user_webhooks %}
                                <tr>
                                    <td>{{ webhook.name }}</td>
                                    <td><a href="{% url 'projects:project_detail' webhook.project.id %}">{{ webhook.project.name }}</a></td>
                                    <td>{{ webhook.get_channel_display }}</td>
                                    <td>
                                        {% if webhook.is_active %}
                                            <span class="badge bg-success">已激活</span>
                                        {% else %}
                                            <span class="badge bg-danger">已禁用</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ webhook.created_at|date:"Y年m月d日 H:i" }}</td>
                                    <td>
                                        <a href="{% url 'projects:project_detail' webhook.project.id %}#webhooks" class="btn btn-sm btn-primary" title="查看详情">
                                            <i class="fa fa-eye" aria-hidden="true"></i>
                                        </a>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% else %}
                <p class="text-center text-muted">暂无Webhook数据</p>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block javascript %}
<script>
    // 添加页面加载动画效果
    document.addEventListener('DOMContentLoaded', function() {
        // 为卡片添加悬停效果
        const cards = document.querySelectorAll('.card');
        cards.forEach(card => {
            card.addEventListener('mouseenter', function() {
                this.style.transform = 'translateY(-2px)';
                this.style.boxShadow = '0 4px 16px rgba(0, 0, 0, 0.12)';
            });
            card.addEventListener('mouseleave', function() {
                this.style.transform = 'translateY(0)';
                this.style.boxShadow = '0 2px 8px rgba(0, 0, 0, 0.08)';
            });
        });
        
        // 为列表项添加平滑过渡效果
        const listItems = document.querySelectorAll('.list-group-item');
        listItems.forEach(item => {
            item.style.transition = 'all 0.2s ease-in-out';
        });
    });
</script>
{% endblock %}